[% setvar title hash slicing %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>hash slicing</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: David Nicol &lt;<a href='mailto:perl6rfc@davidnicol.com'>perl6rfc@davidnicol.com</a>&gt;
  Date: 7 Sep 2000
  Mailing List: <a href='mailto:perl6-language-data@perl.org'>perl6-language-data@perl.org</a>
  Number: 201
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>a more concise syntax for producing subsets of hashes.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<a name='details'></a><h2>details</h2>
<p>Instead of</p>
<pre>	%subhash = map { f($_) ? ($_, $hash{$_}) : () } keys %hash;	# lengthy</pre>
<p>one may now write</p>
<pre>	%subhash = %hash{f($_)};	# code block f($_) will be evaluated for Truth over all the keys</pre>
<a name='syntactically identifiable degenerate cases'></a><h2>syntactically identifiable degenerate cases</h2>
<p>Additionally, these special cases are recognized:</p>
<pre>	%subhash2 = %hash{@a};		# %subhash2 = map {($_, $hash{$_})} @a
	%subhash3 = %hash{$s};		# $subhash3{$s} = $hash{$2}</pre>
<p>Otherwise, <code>%subhash2</code> and <code>%subhash3</code> would become either empty or simple
copies, depending on the Truth of <code>@a</code> and <code>$s</code>, which would be misleading.</p>
<a name='the function may be evaluated &quot;with&quot; the hash as well as &quot;with&quot; the key'></a><h2>the function may be evaluated &quot;with&quot; the hash as well as &quot;with&quot; the key</h2>
<p>In addition to the default scalar <code>$_</code> being set to each key in the hash being
sliced, the &quot;default hash&quot; <code>%_</code> may also be lexically aliased to the hash
being sliced.  In the event that a &quot;with&quot; keyword is adopted which has a
different effect, that effect will occur so that the element selection function
is evaluated for truth for each key, with the hash.</p>
<p>this means that</p>
<pre>	%subhash = map { $hash{$_} =~ /string/ ? ($_, $hash{$_}) : () } keys %hash;</pre>
<p>can be generalized to</p>
<pre>	%subhash = %hash{ $_{$_} =~ /string/ };</pre>
<p>which is more general. All on the same line it looks unnecessary, but if the
filtering function was a big hairy monster and you wanted to create a dozen
subhashes all with different names it would same a lot of copying.</p>
<pre>	%subhashA = %hashA{ Criteria };
	%subhashB = %hashB{ Criteria };
	%subhashC = %hashC{ Criteria };
	%subhashD = %hashD{ Criteria };</pre>
<a name='reasons'></a><h2>reasons</h2>
<p>When I suggested a syntax extension that used</p>
<pre>	%hash_name{something in here}</pre>
<p>it was roundly misread as a data access.  Adoption of this proposal will fill
the curious apparent hole left by the validity of the other slicing operations
in what I hope is a reasonable way.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>The rewrites suggested above suffice as a reference implementation;
optimizations may be possible due to disappearance of various intermediate
temporary storages.</p>
<p>a framework for providing visibility of the magic <code>$_</code> and <code>%_</code> inside the
filtering functions (in threaded environments) will be required.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p><a href='http://www.tmcm.com/tmweb/openingpage/tm6.gif' target='_blank'>www.tmcm.com</a></p>
<p><a href='http://tmcm.com/tmweb/comics/comics111_121/I20_freecoffee.gif' target='_blank'>tmcm.com</a></p>
</div>
